﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using Omes.Model;
using LyData.Data;
using LyData.Data.Xml;

namespace Omes.Services
{
    public class DbServer
    {
        #region  
        public bool UpdatePageInfo(string meta, string footInfo, string siteTitle, string siteName, string metaDescribe, string versionInfo)
        {
            bool result = false;
            try
            {
                DbBase db1 = new DbBase();
                string cmd = "delete from PageInfo";
                db1.ExcuteDb(cmd);
                DbBase db2 = new DbBase();
                string cmd2 = "insert into PageInfo(Meta, FootInfo, SiteTitle, SiteName, MetaDescribe, VersionInfo) values('" + meta + "', '" + footInfo + "', '" + siteTitle +
                    "', '" + siteName + "', '" + metaDescribe + "', '" + versionInfo + "')";
                result = db2.ExcuteDb(cmd2);
            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }
        public bool DeleteFriendLink(int id)
        {
            bool result = false;
            try
            {
                DbBase db = new DbBase();
                string cmd = "delete from FriendLink where ID = " + id;
                result = db.ExcuteDb(cmd);
            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }

        public bool UpdateFriendLink(int id, string title, string url)
        {
            bool result = false;
            try
            {
                DbBase db = new DbBase();
                string cmd = "update FriendLink set Title = '" + title + "', Url = '" + url + "' where ID = " + id;
                result = db.ExcuteDb(cmd);
            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }

        public int AddFriendLink(string title, string url)
        {
            int result = -1;
            try
            {
                DbBase db = new DbBase();
                string cmd = "insert into FriendLink(Title, [Url]) values('" + title + "','" + url + "')";
                result = db.ExcuteInsertDb(cmd);
            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }
        #region AdminInfo
        public AdminInfo GetAdminInfo(int userId)
        {
            AdminInfo result = new AdminInfo();
            try
            {
                DbBase db = new DbBase();
                string cmd = "select * from AdminInfo where ID = " + userId;
                DataSet dataSet = db.GetDataSet(cmd);
                result = this.PartitialGetAdminInfo(dataSet).FirstOrDefault();
            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }

        public bool UpdateAdminInfo(string adminName, string displayName, string password)
        {
            bool result = false;
            try
            {
                DbBase db1 = new DbBase();
                string cmd = "delete from AdminInfo where AdminName = '" + adminName + "'";
                bool temp = db1.ExcuteDb(cmd);
                DbBase db2 = new DbBase();
                string cmd1 = "insert into AdminInfo(AdminName, DisplayName, [Password]) values('" + adminName + "', '" + displayName + "', '" + password + "')";
                result = db2.ExcuteDb(cmd1);
            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }

        public bool TrueUpdateAdminInfo(int id, string adminName, string displayName, string password)
        {
            bool result = false;
            try
            {
                DbBase db = new DbBase();
                string cmd = "update AdminInfo set AdminName = '" + adminName + "', DisplayName = '" + displayName + "', [Password] = '" + password + "' where ID = " + id;
                result = db.ExcuteDb(cmd);
            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }

        public bool DeleteAdminInfo(int userId)
        {
            bool result = false;
            try
            {
                DbBase db = new DbBase();
                string cmd = "delete from AdminInfo where ID = " + userId;
                result = db.ExcuteDb(cmd);
            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }

        public int AddAdminInfo(string adminName, string displayName, string password)
        {
            int result = -1;
            try
            {
                DbBase db1 = new DbBase();
                string cmd1 = "select * from AdminInfo where AdminName = '" + adminName + "'";
                DataSet dataSet = db1.GetDataSet(cmd1);
                List<AdminInfo> temps = this.PartitialGetAdminInfo(dataSet);
                if (temps.Count > 0)
                {
                    result = -1;
                }
                else
                {
                    DbBase db = new DbBase();
                    string cmd = "insert into AdminInfo(AdminName, DisplayName, [Password]) values('" + adminName + "', '" + displayName + "', '" + password + "')";
                    result = db.ExcuteInsertDb(cmd);
                }

            }
            catch (Exception)
            {
                result = -1;
            }
            return result;
        }

        public List<AdminInfo> GetAllAdminInfos()
        {
            List<AdminInfo> result = new List<AdminInfo>();
            try
            {
                DbBase db = new DbBase();
                string cmd = "select * from AdminInfo";
                DataSet dataSet = db.GetDataSet(cmd);
                result = this.PartitialGetAdminInfo(dataSet);
            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }

        public List<AdminInfo> GetAdminInfo(string userName)
        {
            List<AdminInfo> result = new List<AdminInfo>();
            try
            {
                DbBase db = new DbBase();
                string cmd = "select * from AdminInfo where AdminName = '" + userName + "'";
                DataSet dataSet = db.GetDataSet(cmd);
                result = this.PartitialGetAdminInfo(dataSet);
            }
            catch (Exception)
            {

            }
            return result;
        }

        private List<AdminInfo> PartitialGetAdminInfo(DataSet dataSet)
        {
            List<AdminInfo> result = new List<AdminInfo>();
            if (dataSet != null)
            {
                DataTable table = dataSet.Tables[0];
                foreach (DataRow dataRow in table.Rows)
                {
                    AdminInfo adminInfo = new AdminInfo();
                    adminInfo.AdminName = dataRow["AdminName"] == DBNull.Value
                                              ? string.Empty
                                              : dataRow["AdminName"].ToString();
                    adminInfo.DisplayName = dataRow["DisplayName"] == DBNull.Value
                                                ? string.Empty
                                                : dataRow["DisplayName"].ToString();
                    adminInfo.Id = dataRow["ID"] == DBNull.Value ? -1 : Convert.ToInt32(dataRow["ID"]);
                    adminInfo.Password = dataRow["Password"] == DBNull.Value
                                             ? string.Empty
                                             : dataRow["Password"].ToString();
                    result.Add(adminInfo);
                }
            }
            return result;
        }

        #endregion
        #region LoginRecord
        public LoginRecord GetCurrentLoginRecord()
        {
            LoginRecord result = new LoginRecord();
            try
            {
                DbBase db = new DbBase();
                string cmd = "select l.* from LoginRecord l,(select Max(LoginTime) as LoginTime from LoginRecord) m where l.LoginTime = m.LoginTime";
                DataSet dataSet = db.GetDataSet(cmd);
                result = this.PartitialLoginRecord(dataSet);
            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }

        private LoginRecord PartitialLoginRecord(DataSet dataSet)
        {
            LoginRecord result = new LoginRecord();
            try
            {
                DataRowCollection rows = dataSet.Tables[0].Rows;
                foreach (DataRow dataRow in rows)
                {
                    result.Id = dataRow["ID"] is DBNull ? -1 : Convert.ToInt32(dataRow["ID"]);
                    result.LoginIp = dataRow["LoginIp"] is DBNull ? string.Empty : dataRow["LoginIp"].ToString();
                    result.LoginTime = dataRow["LoginTime"] is DBNull ? new DateTime() : DateTime.Parse(dataRow["LoginTime"].ToString());
                    result.UserId = dataRow["UserId"] is DBNull ? -1 : Convert.ToInt32(dataRow["UserId"]);
                }
            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }

        private LoginRecord ParLoginRecord(DataRow dataRow)
        {
            LoginRecord result = new LoginRecord();
            try
            {
                result.Id = dataRow["ID"] is DBNull ? -1 : Convert.ToInt32(dataRow["ID"]);
                result.LoginIp = dataRow["LoginIp"] is DBNull ? string.Empty : dataRow["LoginIp"].ToString();
                result.LoginTime = dataRow["LoginTime"] is DBNull ? new DateTime() : DateTime.Parse(dataRow["LoginTime"].ToString());
                result.UserId = dataRow["UserId"] is DBNull ? -1 : Convert.ToInt32(dataRow["UserId"]);
            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }

        public int AddLoinRecord(int userId, string loginIp, DateTime dateTime)
        {
            int result = -1;
            try
            {
                DbBase db = new DbBase();
                string cmd = "insert into LoginRecord(UserId, LoginIp, LoginTime) values(" + userId + ",'" + loginIp + "','" + dateTime + "')";
                result = db.ExcuteInsertDb(cmd);
            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }

        public List<LoginRecord> GetLoginRecords(int index, int count)
        {
            List<LoginRecord> result = new List<LoginRecord>();
            try
            {
                DbBase db = new DbBase();
                string cmd = "select * from LoginRecord";
                DataSet dataSet = db.GetDataSet(cmd);
                DataRowCollection rows = dataSet.Tables[0].Rows;
                int allnums = rows.Count;
                if ((index + 1) <= allnums)
                {
                    for (int i = index; i < (index + count); i++)
                    {
                        if ((i + 1) <= allnums)
                        {
                            LoginRecord temp = this.ParLoginRecord(rows[i]);
                            result.Add(temp);
                        }
                    }
                }
            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }

        public int GetLoginRecordCount()
        {
            int result = 0;
            try
            {
                DbBase db = new DbBase();
                string cmd = "select count(*) from LoginRecord";
                DataSet dataSet = db.GetDataSet(cmd);
                result = Convert.ToInt32(dataSet.Tables[0].Rows[0][0]);
            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }

        public bool DeleteLoginRecord(int id)
        {
            bool result = false;
            try
            {
                DbBase db = new DbBase();
                string cmd = "delete from LoginRecord where ID = " + id;
                result = db.ExcuteDb(cmd);
            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }

        #endregion
        #region content
        public int GetContentCount(string tableName)
        {
            int result = 0;
            try
            {
                DbBase db = new DbBase();
                string cmd = "select count(*) from " + tableName;
                DataSet dataSet = db.GetDataSet(cmd);
                result = Convert.ToInt32(dataSet.Tables[0].Rows[0][0]);
            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }

        #endregion
        public int AddImage(string title, string path, string commend, int weight, string describe, string blockName, int belongId)
        {
            int result = -1;
            try
            {
                DbBase db = new DbBase();
                string cmd = "insert into ImageList(Title, Path, BelongPageId, Commend, Weight, [Describe], BlockName) values('" + title
                    + "', '" + path + "', " + belongId + ", '" + commend + "', " + weight + ", '" + describe + "', '" + blockName + "')";
                result = db.ExcuteInsertDb(cmd);
            }
            catch (Exception)
            {
                result = -1;
            }
            return result;
        }
        public bool UpdateImage(string title, string path, string commend, int weight, string describe, string blockName, int id)
        {
            bool result = false;
            try
            {
                DbBase db = new DbBase();
                string cmd = "update ImageList set Title = '" + title + "', Path = '" + path + "', Commend = '" + commend + "', Weight = " + weight + ", BlockName = '" + blockName + "', [Describe] = '" + describe + "' where [ID] = " + id;
                result = db.ExcuteDb(cmd);
            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }

        public bool DeleteImage(int id)
        {
            bool result = false;
            try
            {
                DbBase db = new DbBase();
                string cmd = "delete from ImageList where ID = " + id;
                result = db.ExcuteDb(cmd);
            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }

        public bool UpdateNews(int id, string title, string content, string commend, int weight, string describe, string blockName)
        {
            bool result = false;
            try
            {
                DbBase db = new DbBase();
                string cmd = "update NewsList set Title = '" + title + "', Content = '" + content + "', Commend = '" + commend + "', Weight = " + weight + ", BlockName = '" + blockName + "', [Describe] = '" + describe + "' where [ID] = " + id;
                result = db.ExcuteDb(cmd);
            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }

        public bool DeleteNews(int id)
        {
            bool result = false;
            try
            {
                DbBase db = new DbBase();
                string cmd = "delete from NewsList where ID = " + id;
                result = db.ExcuteDb(cmd);
            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }

        public int AddNews(string title, string content, string commend, int weight, string describe, string blockName, int belongId)
        {
            int result = -1;
            try
            {
                DbBase db = new DbBase();
                string cmd = "insert into NewsList(Title, Content, BelongPageId, Commend, Weight, [Describe], BlockName) values('" + title
                    + "', '" + content + "', " + belongId + ", '" + commend + "', " + weight + ", '" + describe + "', '" + blockName + "')";
                result = db.ExcuteInsertDb(cmd);
            }
            catch (Exception)
            {
                result = -1;
            }
            return result;
        }
        public bool UpdateHtml(int id, string title, string content, string commend, int weight, string describe, string blockName)
        {
            bool result = false;
            try
            {
                DbBase db = new DbBase();
                string cmds = "select count(*) from Html where BelongPageId = " + id;
                DataSet dataSet = db.GetDataSet(cmds);
                int count = Convert.ToInt32(dataSet.Tables[0].Rows[0][0]);
                string cmd = string.Empty;
                if (count == 1)
                {
                    cmd = "update Html set Title = '" + title + "', Content = '" + content + "', Commend = '" + commend + "', Weight = " + weight + ", BlockName = '" + blockName + "', [Describe] = '" + describe + "' where BelongPageId = " + id;
                }
                else if (count == 0)
                {
                    cmd =
                        "insert into Html(Title, Content, Commend, Weight, BlockName, [Describe], BelongPageId) values('" + title + "', '" + content + "', '" + commend + "', " + weight + ", '" + blockName + "', '" + describe + "', " + id + ")";
                }
                DbBase db1 = new DbBase();
                result = db1.ExcuteDb(cmd);
            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }

        /// <summary>
        /// use to news and siglephototext
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public List<Omes.Model.Html> GetPageHtmls(int id)
        {
            List<Html> result = new List<Html>();
            try
            {
                DbBase db = new DbBase();
                string cmd = "select * from Html where BelongPageId = " + id;
                DataSet dataSet = db.GetDataSet(cmd);
                result = this.PartitialGetPageHtmls(dataSet);
            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }

        private List<Omes.Model.Html> PartitialGetPageHtmls(DataSet dataSet)
        {
            List<Html> result = new List<Html>();
            DataTable dataTable = dataSet.Tables[0];
            foreach (DataRow dataRow in dataTable.Rows)
            {
                try
                {
                    Html temp = new Html();
                    temp.BelongPageId = dataRow["BelongPageId"] is DBNull ? -1 : Convert.ToInt32(dataRow["BelongPageId"]);
                    temp.BlockName = dataRow["BlockName"] is DBNull ? string.Empty : dataRow["BlockName"].ToString();
                    temp.Commend = dataRow["Commend"] is DBNull ? string.Empty : dataRow["Commend"].ToString();
                    temp.Content = dataRow["Content"] is DBNull ? string.Empty : dataRow["Content"].ToString();
                    temp.Describe = dataRow["Describe"] is DBNull ? string.Empty : dataRow["Describe"].ToString();
                    temp.Id = dataRow["ID"] is DBNull ? -1 : Convert.ToInt32(dataRow["ID"]);
                    temp.Kind = dataRow["Kind"] is DBNull ? string.Empty : dataRow["Kind"].ToString();
                    temp.Title = dataRow["Title"] is DBNull ? string.Empty : dataRow["Title"].ToString();
                    temp.Weight = dataRow["Weight"] is DBNull ? -1 : Convert.ToInt32(dataRow["Weight"]);
                    result.Add(temp);
                }
                catch (Exception)
                {

                    throw;
                }
            }
            return result;
        }

        public bool DeleteHtml(int id)
        {
            bool result = false;
            try
            {
                DbBase db = new DbBase();
                string cmd = "delete from Html where ID = " + id;
                result = db.ExcuteDb(cmd);
            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }
        public bool CheckUser(string userName, string password)
        {
            LyData.Data.IDb idb = new LyData.Data.DbFactory().DriverDb();
            bool result = false;
            try
            {
                if (string.IsNullOrEmpty(userName) || string.IsNullOrEmpty(password))
                {
                    result = false;
                }
                else
                {
                    DbBase db = new DbBase();
                    string cmd = "select * from AdminInfo where AdminName = '" + userName + "' and Password = '" + password + "'";
                    DataSet dataSet = db.GetDataSet(cmd);
                    List<AdminInfo> temps = this.PartitialGetAdminInfo(dataSet);
                    if (temps.Count == 1)
                    {
                        result = true;
                    }
                }
            }
            catch (Exception)
            {
                result = false;
            }
            return result;
        }

        public List<Dictionary<string, string>> GetBarConents()
        {
            List<Dictionary<string, string>> result = new List<Dictionary<string, string>>();
            try
            {
                List<Page> pages = this.GetAllPages();
                result = this.PartitialGetBarContents(pages);
            }
            catch (Exception)
            {
                result = null;
            }
            return result;
        }

        private List<Dictionary<string, string>> PartitialGetBarContents(List<Page> pages)
        {
            List<Dictionary<string, string>> result = new List<Dictionary<string, string>>();
            foreach (Page page in pages)
            {
                if (page.Id > 0)
                {
                    Dictionary<string, string> temp = new Dictionary<string, string>();
                    temp.Add("Id", page.Id.ToString());
                    temp.Add("Name", page.PageName);
                    temp.Add("LinkUrl", page.LinkUrl);
                    result.Add(temp);
                }
            }
            return result;
        }
 

        #region 没有加入页面页面类型联系表之前，获取页面类型（之前定义页面只有一种类型，新的可以一个页面中有多个页面类型）

        public List<PageType> GetAllPageTypes()
        {
            List<PageType> pageTypes = new List<PageType>();
            try
            {
                DbBase db = new DbBase();
                string cmd = "select * from PageType";
                DataSet dataSet = db.GetDataSet(cmd);
                pageTypes = this.PartitialDataSetToListPageType(dataSet);
            }
            catch (Exception)
            {

            }
            return pageTypes;
        }

        #endregion

        public List<Page> GetAllPages()
        {
            List<Page> result = new List<Page>();
            try
            {
                DbBase db = new DbBase();
                string cmd = "select * from Page";
                DataSet dataSet = db.GetDataSet(cmd);
                result = this.PartitialGetAllPages(dataSet);
            }
            catch (Exception)
            {
                result = null;
            }
            return result;
        }
        

        public List<Page> GetParentPages()
        {
            List<Page> result = new List<Page>();
            try
            {
                DbBase db = new DbBase();
                string cmd = "select * from Page where ParentId = 1";
                DataSet dataSet = db.GetDataSet(cmd);
                result = this.PartitialGetAllPages(dataSet);
            }
            catch (Exception)
            {

            }
            return result;
        }

        public int AddPage(string title, int parentId, int typeId, string pageDescribe, string phoPath, string phoContent)
        {
            int result = -1;
            try
            {
                DbBase db = new DbBase();
                string cmd = "insert into Page(PageName, PageTypeId, ParentId, PageDescribe, SmallPhotoPath, Bac1) values('" + title + "'," + typeId + "," + parentId + ", '" + pageDescribe + "', '" + phoPath + "', '" + phoContent + "')";
                result = db.ExcuteInsertDb(cmd);
            }
            catch (Exception)
            {
                result = -1;
            }
            return result;
        }

        public bool UpdatePage(int id, int type, int parent, string bar, string window)
        {
            bool result = false;
            string cmd = "update Page set PageTypeId = " + type + ", ParentId = " + parent + ", ShowOnNar = '" + bar + "', NewWindow = '" + window + "' where ID = " + id;
            try
            {
                DbBase db = new DbBase();
                result = db.ExcuteDb(cmd);
            }
            catch (Exception)
            {
                result = false;
            }
            return result;
        }

        public bool UpdatePageNew(int id, int type, string name, string smallPhoPath, string pageDescribe, string phoContent)
        {
            bool result = false;
            string cmd = "update Page set PageTypeId = " + type + ", PageName = '" + name + "', SmallPhotoPath = '" + smallPhoPath + "', PageDescribe = '" + pageDescribe + "', Bac1 = '" + phoContent + "' where ID = " + id;
            try
            {
                DbBase db = new DbBase();
                result = db.ExcuteDb(cmd);
            }
            catch (Exception)
            {
                result = false;
            }
            return result;
        }

        public bool UpdatePageName(int id, string name)
        {
            bool result = false;
            try
            {
                DbBase db = new DbBase();
                string cmd = "update Page set PageName = '" + name + "' where ID = " + id;
                result = db.ExcuteDb(cmd);
            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }

        public bool DeletePage(int id)
        {
            bool result = false;
            try
            {
                string cmd = "delete from Page where ID = " + id;
                DbBase db = new DbBase();
                result = db.ExcuteDb(cmd);
            }
            catch (Exception)
            {
                result = false;
            }
            return result;
        }

        public PageType GetPageType(int id)
        {
            PageType result = new PageType();
            try
            {
                DbBase db = new DbBase();
                string cmd = "select * from PageType where ID = " + id;
                DataSet dataSet = db.GetDataSet(cmd);
                result = this.PatitialGetPageType(dataSet);
            }
            catch (Exception)
            {

            }
            return result;
        }

        private PageType PatitialGetPageType(DataSet dataSet)
        {
            PageType result = new PageType();
            try
            {
                DataTable table = dataSet.Tables[0];
                foreach (DataRow dataRow in table.Rows)
                {
                    result.Id = dataRow["ID"] == DBNull.Value ? -1 : Convert.ToInt32(dataRow["ID"]);
                    result.PageTypeName = dataRow["PageTypeName"] == DBNull.Value
                                              ? ""
                                              : dataRow["PageTypeName"].ToString();
                    result.Url = dataRow["Url"] == DBNull.Value ? "" : dataRow["Url"].ToString();
                }
            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }

        private List<PageType> PartitialDataSetToListPageType(DataSet dataSet)
        {
            List<PageType> pageTypes = new List<PageType>();
            if (dataSet.Tables.Count > 0)
            {
                DataTable dataTable = dataSet.Tables[0];
                foreach (DataRow dataRow in dataTable.Rows)
                {
                    PageType pageType = new PageType();
                    pageType.Id = dataRow["ID"] == DBNull.Value ? -1 : Convert.ToInt32(dataRow["ID"]);
                    pageType.PageTypeName = dataRow["PageTypeName"] == DBNull.Value
                                                ? string.Empty
                                                : dataRow["PageTypeName"].ToString();
                    pageType.Url = dataRow["Url"] == DBNull.Value ? string.Empty : dataRow["PageTypeName"].ToString();
                    pageTypes.Add(pageType);
                }
            }
            return pageTypes;
        }

        public List<PageType> GetPageTypes(int id)
        {
            List<PageType> result = new List<PageType>();
            try
            {
                DbBase db = new DbBase();
                string cmd = "select pt.ID, pt.PageTypeName, pt.Url from PageType pt, PageRelaPageType prpt where prpt.PageId = " + id + " and prpt.PageTypeId = pt.ID";
                DataSet dataSet = db.GetDataSet(cmd);
                result = this.PartitialDataSetToListPageType(dataSet);
            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }

        #endregion


        private void ConvertPageid(int pageid, out int menuid, out int classid)
        {
            menuid = pageid / 10000000;
            classid = pageid - menuid * 10000000;
        }
        #region Page

        public List<Omes.Model.ImageList> GetAds()
        {
            List<Omes.Model.ImageList> ads = new List<ImageList>();
            var db = new DbBase();
            try
            {
                string cmd = "select * from CodeList where codeclassid = " + 2;
                DataSet dataSet = db.GetDataSet(cmd);
                ads = this.PartitialAds(dataSet);
            }
            catch
            {
                ads = null;

            }
            finally
            {
                db.CloseCon();
            }

            return ads;
        }
        private List<Omes.Model.ImageList> PartitialAds(DataSet dataSet)
        {
            List<Omes.Model.ImageList> ads = new List<ImageList>();
            if (dataSet.Tables.Count > 0)
            {
                DataTable table = dataSet.Tables[0];
                foreach (DataRow dataRow in table.Rows)
                {
                    try
                    {
                        var page = new Omes.Model.ImageList();
                        page.Title = dataRow["Title"] == DBNull.Value ? "" : dataRow["Title"].ToString();
                        page.Path = dataRow["ImgPath"] == DBNull.Value ? "" : dataRow["ImgPath"].ToString();
                        page.BlockName = dataRow["CUrl"] is DBNull ? string.Empty : dataRow["CUrl"].ToString();
                     
                        ads.Add(page);
                    }
                    catch (Exception)
                    { }
                }
            }
            return ads;
        }
        
        public Page GetPage(int id)
        {
            Page result = new Page();
            try
            {
                var db=new DbBase();
                int menuid = 0;
                int classid = 0;
                ConvertPageid(id, out menuid, out  classid);
               
                string cmd = "select * from menu where menuid = " + menuid;
                DataSet dataSet = db.GetDataSet(cmd);
                var row = dataSet.Tables[0].Rows[0];
                if (row != null)
                {
                    var attr = row["Attributer"] == DBNull.Value ? -1 : Convert.ToInt32(row["Attributer"]);
                    //图片类表
                    if (attr == 2)
                    {
                        dataSet = db.GetDataSet(string.Format("select Menuid*10000000+PicClassId as ID,ClassName as PageName,PageDescribe,SmallPhotoPath,Menuid*10000000+ParentId as ParentId from picClass where  picclassid={0}", classid));
                    }
                    //新闻列表
                    if (attr == 1)
                    {
                        dataSet = db.GetDataSet(string.Format("select Menuid*10000000+AtricleClassId as ID,ClassName as PageName,PageDescribe,SmallPhotoPath,Menuid*10000000+ParentId as ParentId  from picClass where  AtricleClassId={0}", classid));
                    }
                    //html
                    if (attr == 0)
                    {
                        dataSet = db.GetDataSet(string.Format("select Menuid*10000000+SinglePageID as ID,Title as PageName,Menuid*10000000+ParentId as ParentId, PageDescribe,'' as SmallPhotoPath from singlepage where  SinglePageID={0}", classid));
                    }
                    result = this.PartitialGetPage(dataSet);
                    result.PageTypeId = attr;
                }
               
            }
            catch (Exception)
            {
                result = null;
            }
            return result;
        }
        private List<Page> PartitialGetAllPages(DataSet dataSet)
        {
            List<Page> result = new List<Page>();
            if (dataSet.Tables.Count > 0)
            {
                DataTable table = dataSet.Tables[0];
                foreach (DataRow dataRow in table.Rows)
                {
                    try
                    {
                        Page page = new Page();
                        page.Id = dataRow["ID"] == DBNull.Value ? -1 : Convert.ToInt32(dataRow["ID"]);
                        // page.LinkType = dataRow["LinkType"] == DBNull.Value ? "" : dataRow["LinkType"].ToString();
                        // page.LinkUrl = dataRow["LinkUrl"] == DBNull.Value ? "" : dataRow["LinkUrl"].ToString();
                        //page.NewWindow = dataRow["NewWindow"] == DBNull.Value ? "" : dataRow["NewWindow"].ToString();
                        page.PageName = dataRow["PageName"] == DBNull.Value ? "" : dataRow["PageName"].ToString();
                        // page.ShowOnNar = dataRow["ShowOnNar"] == DBNull.Value ? "" : dataRow["ShowOnNar"].ToString();
                        page.ParentId = dataRow["ParentId"] == DBNull.Value ? -1 : Convert.ToInt32(dataRow["ParentId"]);
                       // page.PageTypeId = dataRow["PageTypeId"] == DBNull.Value
                        //                       ? 1
                        //                       : Convert.ToInt32(dataRow["PageTypeId"]);
                        page.PageDescribe = dataRow["PageDescribe"] is DBNull ? string.Empty : dataRow["PageDescribe"].ToString();
                        page.SmallPhotoPath = dataRow["SmallPhotoPath"] is DBNull ? string.Empty : dataRow["SmallPhotoPath"].ToString();
                        //page.Bac1 = dataRow["Bac1"] is DBNull ? string.Empty : dataRow["Bac1"].ToString();
                        result.Add(page);
                    }
                    catch (Exception)
                    { }
                }
            }
            return result;
        }

        private Page PartitialGetPage(DataSet dataSet)
        {
            Page page = new Page();
            foreach (DataRow dataRow in dataSet.Tables[0].Rows)
            {
                page.Id = dataRow["ID"] == DBNull.Value ? -1 : Convert.ToInt32(dataRow["ID"]);
               // page.LinkType = dataRow["LinkType"] == DBNull.Value ? "" : dataRow["LinkType"].ToString();
               // page.LinkUrl = dataRow["LinkUrl"] == DBNull.Value ? "" : dataRow["LinkUrl"].ToString();
               // page.NewWindow = dataRow["NewWindow"] == DBNull.Value ? "" : dataRow["NewWindow"].ToString();
                page.PageName = dataRow["PageName"] == DBNull.Value ? "" : dataRow["PageName"].ToString();
               // page.ShowOnNar = dataRow["ShowOnNar"] == DBNull.Value ? "" : dataRow["ShowOnNar"].ToString();
                page.ParentId = dataRow["ParentId"] == DBNull.Value ? -1 : Convert.ToInt32(dataRow["ParentId"]);
               // page.PageTypeId = dataRow["PageTypeId"] == DBNull.Value
               //                       ? 1
                //                      : Convert.ToInt32(dataRow["PageTypeId"]);
                page.PageDescribe = dataRow["PageDescribe"] is DBNull ? string.Empty : dataRow["PageDescribe"].ToString();
                page.SmallPhotoPath = dataRow["SmallPhotoPath"] is DBNull ? string.Empty : dataRow["SmallPhotoPath"].ToString();
               // page.Bac1 = dataRow["Bac1"] is DBNull ? string.Empty : dataRow["Bac1"].ToString();
            }

            return page;
        }

        public List<Page> UseParentIdGetPages(int id)
        {
            List<Page> result = new List<Page>();
            try
            {
                var db = new DbBase();
                int menuid = 0;
                int classid = 0;
                ConvertPageid(id, out menuid, out  classid);

                string cmd = "select * from menu where menuid = " + menuid;
                DataSet dataSet = db.GetDataSet(cmd);
                var row = dataSet.Tables[0].Rows[0];
                if (row != null)
                {
                    var attr = row["Attributer"] == DBNull.Value ? -1 : Convert.ToInt32(row["Attributer"]);
                    //图片类表
                    if (attr == 2)
                    {
                        dataSet = db.GetDataSet(string.Format("select Menuid*10000000+PicClassId as ID,ClassName as PageName,PageDescribe,SmallPhotoPath,Menuid*10000000+ParentId as ParentId from picClass where ParentId={0}", classid));
                    }
                    //新闻列表
                    if (attr == 1)
                    {
                        dataSet = db.GetDataSet(string.Format("select Menuid*10000000+AritcleClassId as ID,ClassName as PageName,PageDescribe,SmallPhotoPath,Menuid*10000000+ParentId as ParentId from  articleClass where  ParentID={0}", classid));
                    }
                    //html
                    if (attr == 0)
                    {
                        dataSet = db.GetDataSet(string.Format("select Menuid*10000000+SinglePageID as ID,Title as PageName,PageDescribe,'' as SmallPhotoPath,Menuid*10000000+ParentId as ParentId from singlePage where ParentID={0}", classid));
                    }
                    result = this.PartitialGetAllPages(dataSet);
                    result.ForEach(i=>i.PageTypeId = attr);
                }
                
            }
            catch (Exception)
            {
                result = null;
            }
            return result;
        }

        #endregion

        #region Html

        public Omes.Model.Html GetHtmlById(int htmlid)
        {
            Html result = new Html();
            try
            {
                var db = new DbBase();
                string cmd = string.Format("select SinglePageid id, PageContent Content,Title from singlePage where singlePageId ={0} ", htmlid);
                DataSet dataSet = db.GetDataSet(cmd);
                result = this.PartitialGetPageHtml(dataSet);
            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }
        public Omes.Model.Html GetPageHtml(int id)
        {
            Html result = new Html();
            try
            {
                var db = new DbBase();
                int menuid = 0;
                int classid = 0;
                ConvertPageid(id, out menuid, out  classid);
                string cmd = string.Format("select SinglePageid as id, PageContent as Content,Title,PageDescribe from singlePage where singlePageId ={0} ", classid);
                DataSet dataSet = db.GetDataSet(cmd);
                result = this.PartitialGetPageHtml(dataSet);
            }
            catch (Exception)
            {
                throw;
            }
            return result;
        }

        private Omes.Model.Html PartitialGetPageHtml(DataSet dataSet)
        {
            Html result = new Html();
            try
            {
                DataRow dataRow = dataSet.Tables[0].Rows[0];
                //result.BelongPageId = dataRow["BelongPageId"] is DBNull ? -1 : Convert.ToInt32(dataRow["BelongPageId"]);
                //result.BlockName = dataRow["BlockName"] is DBNull ? string.Empty : dataRow["BlockName"].ToString();
                //result.Commend = dataRow["Commend"] is DBNull ? string.Empty : dataRow["Commend"].ToString();
                result.Content = dataRow["Content"] is DBNull ? string.Empty : dataRow["Content"].ToString();
                result.Describe = dataRow["PageDescribe"] is DBNull ? string.Empty : dataRow["PageDescribe"].ToString();
                result.Id = dataRow["ID"] is DBNull ? -1 : Convert.ToInt32(dataRow["ID"]);
               // result.Kind = dataRow["Kind"] is DBNull ? string.Empty : dataRow["Kind"].ToString();
                result.Title = dataRow["Title"] is DBNull ? string.Empty : dataRow["Title"].ToString();
                //result.Weight = dataRow["Weight"] is DBNull ? -1 : Convert.ToInt32(dataRow["Weight"]);
            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }

        #endregion

        #region NewsList
        public List<Omes.Model.NewsList> GetPageNews(int id)
        {
            List<Omes.Model.NewsList> result = new List<NewsList>();
            try
            {
                var db = new DbBase();
                int menuid = 0;
                int classid = 0;
                ConvertPageid(id, out menuid, out  classid);
                string cmd =string.Format( "select a.articleid as id, a.ArtTJ as Commend,a.ArticleContent as Content,a.Title,b.FileName as kind,PageDesc  from article a,articleclass b where a.articleclassid=b.articleclassid and  a.articleclassid in (select articleclassid from articleclass where parentid={0} or articleclassid={0})",classid);
                DataSet dataSet = db.GetDataSet(cmd);
                result = this.PartitialGetPageNews(dataSet);
            }
            catch (Exception)
            {

               
            }
            return result;
        }

        private List<NewsList> PartitialGetPageNews(DataSet dataSet)
        {
            List<NewsList> result = new List<NewsList>();
            DataTable dataTable = dataSet.Tables[0];
            foreach (DataRow dataRow in dataTable.Rows)
            {
                try
                {
                    NewsList temp = new NewsList();
                   // temp.BelongPageId = dataRow["BelongPageId"] is DBNull ? -1 : Convert.ToInt32(dataRow["BelongPageId"]);
                    temp.Kind = dataRow["kind"] is DBNull ? string.Empty : dataRow["kind"].ToString();
                    temp.Commend = dataRow["Commend"] is DBNull ? string.Empty : dataRow["Commend"].ToString();
                    temp.Content = dataRow["Content"] is DBNull ? string.Empty : dataRow["Content"].ToString();
                    temp.Describe = dataRow["PageDesc"] is DBNull ? string.Empty : dataRow["PageDesc"].ToString();
                    temp.Id = dataRow["ID"] is DBNull ? -1 : Convert.ToInt32(dataRow["ID"]);
                   
                    temp.Title = dataRow["Title"] is DBNull ? string.Empty : dataRow["Title"].ToString();
                   // temp.Weight = dataRow["Weight"] is DBNull ? -1 : Convert.ToInt32(dataRow["Weight"]);
                    result.Add(temp);
                }
                catch (Exception)
                {

                    throw;
                }
            }
            return result;
        }

        public NewsList GetNewsInfo(int id)
        {
            NewsList result = new NewsList();
            try
            {
                DbBase db = new DbBase();
                string cmd = string.Format("select  a.articleid as id, a.ArtTJ as Commend,a.ArticleContent as Content,a.Title,b.FileName as kind,PageDesc  from article a,articleclass b where a.articleid = {0} and a.articleclassid=b.articleclassid", id);
                DataSet dataSet = db.GetDataSet(cmd);
                result = this.PartitialNewsInfo(dataSet);
            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }

        private NewsList PartitialNewsInfo(DataSet dataSet)
        {
            NewsList result = new NewsList();
            try
            {
                DataRow dataRow = dataSet.Tables[0].Rows[0];
               // result.BelongPageId = dataRow["BelongPageId"] is DBNull ? -1 : Convert.ToInt32(dataRow["BelongPageId"]);
                result.BlockName = dataRow["kind"] is DBNull ? string.Empty : dataRow["kind"].ToString();
                result.Commend = dataRow["Commend"] is DBNull ? string.Empty : dataRow["Commend"].ToString();
                result.Content = dataRow["Content"] is DBNull ? string.Empty : dataRow["Content"].ToString();
                result.Describe = dataRow["PageDesc"] is DBNull ? string.Empty : dataRow["PageDesc"].ToString();
                result.Id = dataRow["ID"] is DBNull ? -1 : Convert.ToInt32(dataRow["ID"]);
                result.Kind = dataRow["Kind"] is DBNull ? string.Empty : dataRow["Kind"].ToString();
                result.Title = dataRow["Title"] is DBNull ? string.Empty : dataRow["Title"].ToString();
               // result.Weight = dataRow["Weight"] is DBNull ? -1 : Convert.ToInt32(dataRow["Weight"]);
            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }

       

        #endregion

        #region ImageList
        public List<ImageList> GetImageListOfPage(int pageId)
        {
            List<ImageList> result = new List<ImageList>();
            try
            {
                var db = new DbBase();
                int menuid = 0;
                int classid = 0;
                ConvertPageid(pageId, out menuid, out  classid);
                string cmd = "select picid as id,PicPathBig as Path,Groom as commend,PicContent,Title from Pic where picclassid = " + classid;
                DataSet dataSet = db.GetDataSet(cmd);
                result = this.PartitialGetImageListOfPage(dataSet);
            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }

        private List<ImageList> PartitialGetImageListOfPage(DataSet dataSet)
        {
            List<ImageList> result = new List<ImageList>();
            DataTable dataTable = dataSet.Tables[0];
            foreach (DataRow dataRow in dataTable.Rows)
            {
                try
                {
                    ImageList temp = new ImageList();
                    temp.Id = dataRow["ID"] is DBNull ? -1 : Convert.ToInt32(dataRow["ID"]);
                   // temp.BelongPageId = dataRow["BelongPageId"] is DBNull ? -1 : Convert.ToInt32(dataRow["BelongPageId"]);
                  //  temp.BlockName = dataRow["BlockName"] is DBNull ? string.Empty : dataRow["BlockName"].ToString();
                    temp.Commend = dataRow["Commend"] is DBNull ? string.Empty : dataRow["Commend"].ToString();
                    temp.Describe = dataRow["PicContent"] is DBNull ? string.Empty : dataRow["PicContent"].ToString();
                   // temp.Kind = dataRow["Kind"] is DBNull ? string.Empty : dataRow["Kind"].ToString();
                  //  temp.LinkPageId = dataRow["LinkPageId"] is DBNull ? -1 : Convert.ToInt32(dataRow["LinkPageId"]);
                    temp.Path = dataRow["Path"] is DBNull ? string.Empty : dataRow["Path"].ToString();
                    temp.Title = dataRow["Title"] is DBNull ? string.Empty : dataRow["Title"].ToString();
                   // temp.Weight = dataRow["Weight"] is DBNull ? -1 : Convert.ToInt32(dataRow["Weight"]);
                    result.Add(temp);
                }
                catch (Exception)
                {

                    throw;
                }
            }
            return result;
        }

        public ImageList GetImage(int id)
        {
            ImageList result = new ImageList();
            try
            {
                DbBase db = new DbBase();
                string cmd = "select picid as id,PicPathBig  as Path,Groom as commend,PicContent,Title from pic where picid = " + id;
                DataSet dataSet = db.GetDataSet(cmd);
                result = this.PartitialGetImage(dataSet);
            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }

        private ImageList PartitialGetImage(DataSet dataSet)
        {
            ImageList result = new ImageList();
            try
            {
                foreach (DataRow dataRow in dataSet.Tables[0].Rows)
                {
                    ImageList temp = new ImageList();
                    temp.Id = dataRow["ID"] is DBNull ? -1 : Convert.ToInt32(dataRow["ID"]);
                    //temp.BelongPageId = dataRow["BelongPageId"] is DBNull ? -1 : Convert.ToInt32(dataRow["BelongPageId"]);
                   // temp.BlockName = dataRow["BlockName"] is DBNull ? string.Empty : dataRow["BlockName"].ToString();
                    temp.Commend = dataRow["Commend"] is DBNull ? string.Empty : dataRow["Commend"].ToString();
                    temp.Describe = dataRow["PicContent"] is DBNull ? string.Empty : dataRow["PicContent"].ToString();
                   // temp.Kind = dataRow["Kind"] is DBNull ? string.Empty : dataRow["Kind"].ToString();
                   // temp.LinkPageId = dataRow["LinkPageId"] is DBNull ? -1 : Convert.ToInt32(dataRow["LinkPageId"]);
                    temp.Path = dataRow["Path"] is DBNull ? string.Empty : dataRow["Path"].ToString();
                    temp.Title = dataRow["Title"] is DBNull ? string.Empty : dataRow["Title"].ToString();
                 //   temp.Weight = dataRow["Weight"] is DBNull ? -1 : Convert.ToInt32(dataRow["Weight"]);
                    result = temp;
                }

            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }

       

        #endregion

        #region LeaveMessage
        /// <summary>
        /// 分页接口，获取从index开始的count条数据
        /// </summary>
        /// <param name="index"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public List<LeaveMessage> GetLeaveMessages(int index, int count)
        {
            List<LeaveMessage> result = new List<LeaveMessage>();
            try
            {
                DbBase db = new DbBase();
                string cmd = "select * from LeaveMessage";
                DataSet dataSet = db.GetDataSet(cmd);
                List<LeaveMessage> alls = this.PartitialGetLeaveMessages(dataSet);
                for (int i = index; i < (index + count); i++)
                {
                    if (alls.Count >= (i + 1))
                    {
                        result.Add(alls[i]);
                    }
                }
            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }

        private List<LeaveMessage> PartitialGetLeaveMessages(DataSet dataSet)
        {
            List<LeaveMessage> result = new List<LeaveMessage>();
            DataRowCollection dataRowCollection = dataSet.Tables[0].Rows;
            foreach (DataRow dataRow in dataRowCollection)
            {
                try
                {
                    LeaveMessage temp = new LeaveMessage();
                    temp.Content = dataRow["Content"] is DBNull ? string.Empty : dataRow["Content"].ToString();
                    temp.Id = dataRow["ID"] is DBNull ? -1 : Convert.ToInt32(dataRow["ID"]);
                    temp.LeaveTime = dataRow["LeaveTime"] is DBNull ? string.Empty : dataRow["LeaveTime"].ToString();
                    temp.UserName = dataRow["UserName"] is DBNull ? string.Empty : dataRow["UserName"].ToString();
                    temp.UserPhone = dataRow["UserPhone"] is DBNull ? "" : dataRow["UserPhone"].ToString();
                    try
                    {
                        temp.Type = dataRow["Type"] is DBNull ? 1 : Convert.ToInt32(dataRow["Type"]);
                    }
                    catch (Exception)
                    {
                        temp.Type = 1;
                    }
                    result.Add(temp);
                }
                catch (Exception)
                {

                    throw;
                }
            }
            return result;
        }

        public bool DeleteLeaveMessge(int id)
        {
            bool result = false;
            try
            {
                DbBase db = new DbBase();
                string cmd = "delete from LeaveMessage where ID = " + id;
                result = db.ExcuteDb(cmd);
            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }

        public int AddLeaveMessage(string userName, string content, string leaveTime, string userPhone, int type)
        {
            int reslt = -1;
            try
            {
                DbBase db = new DbBase();
                string cmd = "insert into LeaveMessage(UserName, Content, LeaveTime, UserPhone, [Type]) values('" + userName + "', '" + content + "', '" + leaveTime + "','" + userPhone + "'," + type + ")";
                reslt = db.ExcuteInsertDb(cmd);
            }
            catch (Exception)
            {

                throw;
            }
            return reslt;
        }

        public List<LeaveMessage> GetLeaveMessages(int index, int count, int type)
        {
            List<LeaveMessage> result = new List<LeaveMessage>();
            try
            {
                List<LeaveMessage> temps = new List<LeaveMessage>();
                DbBase db = new DbBase();
                string cmd = "select * from LeaveMessage where [Type] = "+type;
                DataSet dataSet = db.GetDataSet(cmd);
                List<LeaveMessage> alls = this.PartitialGetLeaveMessages(dataSet);
                for (int i = index; i < (index + count); i++)
                {
                    if (alls.Count >= (i + 1))
                    {
                        result.Add(alls[i]);
                    }
                }
            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }

        public int GetLeaveMessageCount(int type)
        {
            int result = -1;
            try
            {
                DbBase db = new DbBase();
                string cmd = "select count(*) from LeaveMessage where [Type] = " + type;
                DataSet dataSet = db.GetDataSet(cmd);
                result = Convert.ToInt32(dataSet.Tables[0].Rows[0][0]);
            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }

        #endregion

        #region JoinUsMessage
        /// <summary>
        /// 分页接口
        /// </summary>
        /// <param name="index"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public List<JoinUsMessage> GetJoinUsMessges(int index, int count)
        {
            List<JoinUsMessage> result = new List<JoinUsMessage>();
            try
            {
                DbBase db = new DbBase();
                string cmd = "select * from JoinUsMessage";
                DataSet dataSet = db.GetDataSet(cmd);
                List<JoinUsMessage> alls = this.PartitialGetJoinUsMessages(dataSet);
                for (int i = index; i < (index + count); i++)
                {
                    if (alls.Count >= (i + 1))
                    {
                        result.Add(alls[i]);
                    }
                }
            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }

        private List<JoinUsMessage> PartitialGetJoinUsMessages(DataSet dataSet)
        {
            List<JoinUsMessage> result = new List<JoinUsMessage>();
            DataRowCollection rows = dataSet.Tables[0].Rows;
            foreach (DataRow dataRow in rows)
            {
                try
                {
                    JoinUsMessage temp = new JoinUsMessage();
                    temp.ContactTime = dataRow["ContactTime"] is DBNull ? string.Empty : dataRow["ContactTime"].ToString();
                    temp.Content = dataRow["Content"] is DBNull ? string.Empty : dataRow["Content"].ToString();
                    temp.UserName = dataRow["UserName"] is DBNull ? string.Empty : dataRow["UserName"].ToString();
                    temp.Id = dataRow["ID"] is DBNull ? -1 : Convert.ToInt32(dataRow["ID"]);
                    temp.UserMobilePhone = dataRow["UserMobilePhone"] is DBNull ? -1 : Convert.ToInt32(dataRow["UserMobilePhone"]);
                    temp.UserStaticPhone = dataRow["UserStaticPhone"] is DBNull ? -1 : Convert.ToInt32(dataRow["UserStaticPhone"]);
                    result.Add(temp);
                }
                catch (Exception)
                {

                    throw;
                }
            }
            return result;
        }

        public bool DeleteJoinUsMessage(int id)
        {
            bool result = false;
            try
            {
                DbBase db = new DbBase();
                string cmd = "delete from JoinUsMessage where ID = " + id;
                result = db.ExcuteDb(cmd);
            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }

        public int AddJoinUsMessage(string userName, int mobilePhone, int staticPhone, string contactTime, string content)
        {
            int result = -1;
            try
            {
                DbBase db = new DbBase();
                string cmd = "insert into JoinUsMessage(UserName, UserMobilePhone, UserStaticPhone, ContactTime, Content) values('" + userName + "', " + mobilePhone + ", " + staticPhone + ", '" + contactTime + "', '" + content + "')";
                result = db.ExcuteInsertDb(cmd);
            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }

        #endregion

        #region FriendLink
        public List<FriendLink> GetFriendLinks(int index=0, int count=0)
        {
            List<FriendLink> result = new List<FriendLink>();
            

            try
            {
                if (index == 0 && count == 0)
                {
                    var db = new DbBase();
                    string cmd = "select FriendLinkID as ID,Title,url from FriendLink";
                    DataSet dataSet = db.GetDataSet(cmd);
                    result = this.PartitialGetFriendLinks(dataSet);
                }
                else
                {
                    var db = new DbBase();
                    string cmd = "select FriendLinkID as ID,Title,url from FriendLink";
                    DataSet dataSet = db.GetDataSet(cmd);
                    List<FriendLink> alls = this.PartitialGetFriendLinks(dataSet);
                    for (int i = index; i < (index + count); i++)
                    {
                        if (alls.Count >= (i + 1))
                        {
                            result.Add(alls[i]);
                        }
                    }
                }
            }
            catch (Exception)
            {
                
            }
            return result;
        }

        private List<FriendLink> PartitialGetFriendLinks(DataSet dataSet)
        {
            List<FriendLink> result = new List<FriendLink>();
            DataRowCollection rows = dataSet.Tables[0].Rows;
            foreach (DataRow dataRow in rows)
            {
                try
                {
                    FriendLink temp = new FriendLink();
                    temp.Id = dataRow["ID"] is DBNull ? -1 : Convert.ToInt32(dataRow["ID"]);
                    temp.Title = dataRow["Title"] is DBNull ? string.Empty : dataRow["Title"].ToString();
                    temp.Url = dataRow["Url"] is DBNull ? string.Empty : dataRow["Url"].ToString();
                    result.Add(temp);
                }
                catch (Exception)
                {
                    
                    throw;
                }
            }
            return result;
        }



        #endregion

        #region PageInfo
        public PageInfo GetPageInfo()
        {
            PageInfo result = new PageInfo();
            try
            {
                DataRow row = new XmlProvider(System.Web.HttpContext.Current.Server.MapPath("~/App_Data/Website.xml")).ReadNode(string.Format("descendant::Info[Language={0}]", 0));
                if (row != null)
                {
                    result.SiteName = (string)row["WebsiteName"];
                   result.SiteTitle = (string)row["WebsiteTitle"];
                    result.Meta = (string)row["KeyWords"];
                    result.MetaDescribe = (string)row["Description"];
                    result.FootInfo = (string)row["CopyRight"];
                }
            }
            catch (Exception)
            {
                
                throw;
            }
            return result;
        }

        private PageInfo PartitialGetPageInfo(DataSet dataSet)
        {
            PageInfo result = new PageInfo();
            try
            {
                DataRowCollection rows = dataSet.Tables[0].Rows;
                foreach (DataRow dataRow in rows)
                {
                    result.FootInfo = dataRow["FootInfo"] is DBNull ? string.Empty : dataRow["FootInfo"].ToString();
                    result.Id = dataRow["ID"] is DBNull ? -1 : Convert.ToInt32(dataRow["ID"]);
                    result.Meta = dataRow["Meta"] is DBNull ? string.Empty : dataRow["Meta"].ToString();
                    result.SiteName = dataRow["SiteName"] is DBNull ? string.Empty : dataRow["SiteName"].ToString();
                    result.SiteTitle = dataRow["SiteTitle"] is DBNull ? string.Empty : dataRow["SiteTitle"].ToString();
                    result.MetaDescribe = dataRow["MetaDescribe"] is DBNull ? string.Empty : dataRow["MetaDescribe"].ToString();
                    result.VersionInfo = dataRow["VersionInfo"] is DBNull ? string.Empty : dataRow["VersionInfo"].ToString();
                }
            }
            catch (Exception)
            {
                
                throw;
            }
            return result;
        }

       

        #endregion

        #region AnonyQuestionAnswer
        private AnonyQuestionQuestionAnswer PartitialAnonyQuestionAnswer(DataRow dataRow)
        {
            AnonyQuestionQuestionAnswer result = new AnonyQuestionQuestionAnswer();
            try
            {
                result.Id = dataRow["ID"] is DBNull ? -1 : Convert.ToInt32(dataRow["ID"]);
                result.AnswerId = dataRow["AnswerId"] is DBNull ? -1 : Convert.ToInt32(dataRow["AnswerId"]);
                result.QuestionId = dataRow["QuestionId"] is DBNull ? -1 : Convert.ToInt32(dataRow["QuestionId"]);
                result.UserId = dataRow["UserId"] is DBNull ? -1 : Convert.ToInt32(dataRow["UserId"]);
                result.AnswerTime = dataRow["AnswerTime"] is DBNull ? string.Empty : dataRow["AnswerTime"].ToString();
                result.AnswerBac = dataRow["AnswerBac"] is DBNull ? string.Empty : dataRow["AnswerBac"].ToString();
            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }

        public List<Anonymous> GetAllQuestionNaireUser()
        {
            List<Anonymous> result = new List<Anonymous>();
            try
            {
                DbBase db = new DbBase();
                string cmd = "select * from Anonymous";
                DataSet dataSet = db.GetDataSet(cmd);
                foreach (DataRow dataRow in dataSet.Tables[0].Rows)
                {
                    try
                    {
                        Anonymous temp = this.PartitialAnonymous(dataRow);
                        if (temp != null)
                        {
                            result.Add(temp);
                        }
                    }
                    catch (Exception)
                    {

                        throw;
                    }
                }
            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }

        public List<Anonymous> GetQuestionNaireUsers(int index, int count)
        {
            List<Anonymous> result = new List<Anonymous>();
            try
            {
                List<Anonymous> alls = this.GetAllQuestionNaireUser();
                if (index < alls.Count)
                {
                    for (int i = index; i < (index + count); i++)
                    {
                        if (i < alls.Count)
                        {
                            Anonymous temp = alls[i];
                            result.Add(temp);
                        }
                    }
                }
            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }

        public int GetQuestionNaireUsersCount()
        {
            int result = 0;
            try
            {
                DbBase db = new DbBase();
                string cmd = "select count(*) from Anonymous";
                result = Convert.ToInt32(db.GetDataSet(cmd).Tables[0].Rows[0][0]);
            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }

        private Anonymous PartitialAnonymous(DataRow dataRow)
        {
            Anonymous result = new Anonymous();
            try
            {
                result.Id = dataRow["ID"] is DBNull ? -1 : Convert.ToInt32(dataRow["ID"]);
                result.UserName = dataRow["UserName"] is DBNull ? string.Empty : dataRow["UserName"].ToString();
            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }

        public List<AnonyQuestionQuestionAnswer> GetQuestionNairesUseUserId(int userId)
        {
            List<AnonyQuestionQuestionAnswer> result = new List<AnonyQuestionQuestionAnswer>();
            try
            {
                DbBase db = new DbBase();
                string cmd = "select * from AnonyQuestionAnswer where UserId = " + userId;
                DataSet dataSet = db.GetDataSet(cmd);
                foreach (DataRow dataRow in dataSet.Tables[0].Rows)
                {
                    try
                    {
                        AnonyQuestionQuestionAnswer temp = this.PartitialAnonyQuestionAnswer(dataRow);
                        if (temp != null)
                        {
                            result.Add(temp);
                        }
                    }
                    catch (Exception)
                    {

                        throw;
                    }
                }
            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }

        public bool DeleteAnonymousUserQuestionNaire(int userId)
        {
            bool result = false;
            try
            {
                DbBase db = new DbBase();
                string cmd = "delete from AnonyQuestionAnswer where UserId = " + userId;
                result = db.ExcuteDb(cmd);
            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }

        public bool DeleteAnonymous(int userId)
        {
            bool result = false;
            try
            {
                DbBase db = new DbBase();
                string cmd = "delete from Anonymous where ID = " + userId;
                result = db.ExcuteDb(cmd);
            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }

        public QuestionNaire GetQuestionNarieUseQuestionId(int questionId)
        {
            QuestionNaire result = new QuestionNaire();
            try
            {
                DbBase db = new DbBase();
                string cmd = "select * from QuestionNaire where ID = " + questionId;
                DataSet dataSet = db.GetDataSet(cmd);
                result = this.PartitialQuestionNaire(dataSet.Tables[0].Rows[0]);
            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }

        private QuestionNaire PartitialQuestionNaire(DataRow dataRow)
        {
            QuestionNaire result = new QuestionNaire();
            try
            {
                result.Id = dataRow["ID"] is DBNull ? -1 : Convert.ToInt32(dataRow["ID"]);
                result.QuestionContent = dataRow["QuestionContent"] is DBNull
                                             ? string.Empty
                                             : dataRow["QuestionContent"].ToString();
                result.Type = dataRow["Type"] is DBNull ? -1 : Convert.ToInt32(dataRow["Type"]);
            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }

        public List<QuestionNaireAnswer> GetQuestionNarieAnswerUseQuestionId(int questionId)
        {
            List<QuestionNaireAnswer> result = new List<QuestionNaireAnswer>();
            try
            {
                DbBase db = new DbBase();
                string cmd = "select * from QuestionNaireAnswer where BelongQuestion = " + questionId;
                DataSet dataSet = db.GetDataSet(cmd);
                foreach (DataRow dataRow in dataSet.Tables[0].Rows)
                {
                    QuestionNaireAnswer temp = this.PartitialQuestionNaireAnswer(dataRow);
                    if (temp != null)
                    {
                        result.Add(temp);
                    }
                }
            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }

        private QuestionNaireAnswer PartitialQuestionNaireAnswer(DataRow dataRow)
        {
            QuestionNaireAnswer result = new QuestionNaireAnswer();
            try
            {
                result.Id = dataRow["ID"] is DBNull ? -1 : Convert.ToInt32(dataRow["ID"]);
                result.Answer = dataRow["Answer"] is DBNull ? string.Empty : dataRow["Answer"].ToString();
            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }

        public List<QuestionNaire> GetAllQuestionNaires()
        {
            List<QuestionNaire> result = new List<QuestionNaire>();
            try
            {
                DbBase db = new DbBase();
                string cmd = "select * from QuestionNaire";
                DataSet dataSet = db.GetDataSet(cmd);
                foreach (DataRow row in dataSet.Tables[0].Rows)
                {
                    QuestionNaire temp = this.PartitialQuestionNaire(row);
                    result.Add(temp);
                }
            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }

        public int GetAllQuestionCount()
        {
            int result = 0;
            result = this.GetAllQuestionNaires().Count;
            return result;
        }

        public List<QuestionNaire> GetQuestions(int index, int count)
        {
            List<QuestionNaire> result = new List<QuestionNaire>();
            try
            {
                List<QuestionNaire> alls = this.GetAllQuestionNaires();
                if (index < alls.Count)
                {
                    for (int i = index; i < (index + count); i++)
                    {
                        if (i < alls.Count)
                        {
                            QuestionNaire temp = alls[i];
                            result.Add(temp);
                        }
                    }
                }
            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }

        public List<AnonyQuestionQuestionAnswer> GetAQQAs(int userId, int questionId)
        {
            List<AnonyQuestionQuestionAnswer> result = new List<AnonyQuestionQuestionAnswer>();
            try
            {
                DbBase db = new DbBase();
                string cmd = "select * from AnonyQuestionAnswer where UserId = " + userId + " and QuestionId = " + questionId;
                DataSet dataSet = db.GetDataSet(cmd);
                foreach (DataRow dataRow in dataSet.Tables[0].Rows)
                {
                    AnonyQuestionQuestionAnswer temp = this.PartitialAnonyQuestionAnswer(dataRow);
                    if (temp != null)
                    {
                        result.Add(temp);
                    }
                }
            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }

        public QuestionNaireAnswer GetQuestionNaireAnswer(int answerId)
        {
            QuestionNaireAnswer result = new QuestionNaireAnswer();
            try
            {
                DbBase db = new DbBase();
                string cmd = "select * from QuestionNaireAnswer where ID = " + answerId;
                DataSet dataSet = db.GetDataSet(cmd);
                result = this.PartitialQuestionNaireAnswer(dataSet.Tables[0].Rows[0]);
            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }

        public List<string> GetUserQuestionsAnswers(int userId, int questionId)
        {
            List<string> result = new List<string>();
            try
            {
                QuestionNaire questionNaire = this.GetQuestionNarieUseQuestionId(questionId);
                switch (questionNaire.Type)
                {
                    case 1:
                        {
                            List<AnonyQuestionQuestionAnswer> aqqas = this.GetAQQAs(userId, questionId);
                            foreach (AnonyQuestionQuestionAnswer anonyQuestionQuestionAnswer in aqqas)
                            {
                                string answer = this.GetQuestionNaireAnswer(anonyQuestionQuestionAnswer.AnswerId).Answer;
                                result.Add(answer);
                            }
                        } break;
                    case 2:
                        {
                            List<AnonyQuestionQuestionAnswer> aqqas = this.GetAQQAs(userId, questionId);
                            foreach (AnonyQuestionQuestionAnswer anonyQuestionQuestionAnswer in aqqas)
                            {
                                string answer = this.GetQuestionNaireAnswer(anonyQuestionQuestionAnswer.AnswerId).Answer;
                                result.Add(answer);
                            }
                        } break;
                    case 3:
                        {
                            List<AnonyQuestionQuestionAnswer> aqqas = this.GetAQQAs(userId, questionId);
                            foreach (AnonyQuestionQuestionAnswer anonyQuestionQuestionAnswer in aqqas)
                            {
                                string answer = anonyQuestionQuestionAnswer.AnswerBac;
                                result.Add(answer);
                            }
                        } break;
                }
            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }

        #endregion

        #region Fore-Quesiton
        public List<QuestionNaire> GetAllQuestions(int type)
        {
            List<QuestionNaire> result = new List<QuestionNaire>();
            List<QuestionNaire> temps = this.GetAllQuestionNaires();
            result = temps.Where(t => (t.Type == type)).ToList();
            return result;
        }

        public int AddQuestionAnswerSM(int userId, int qustionId, int answerId)
        {
            int result = -1;
            try
            {
                DbBase db = new DbBase();
                string cmd = "insert into AnonyQuestionAnswer(UserId, QuestionId, AnswerId, AnswerTime) values(" + userId + ", " + qustionId + ", " + answerId + ", '" + DateTime.Now.ToString() + "')";
                result = db.ExcuteInsertDb(cmd);
            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }

        public int AddQuestionAnswerS(int userId, int questionId, string answer)
        {
            int result = -1;
            try
            {
                DbBase db = new DbBase();
                string cmd = "insert into AnonyQuestionAnswer(UserId, QuestionId, AnswerBac, AnswerTime) values(" + userId + ", " + questionId + ", '" + answer + "', '" + DateTime.Now.ToString() + "')";
                result = db.ExcuteInsertDb(cmd);
            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }

        #endregion

    }
}
